#/bin/bash

# 备份目录，需提前创建
BACKUP_DIR='/root/mysql-backup'

# 备份MySQL数据库
[ -d "${BACKUP_DIR}" ] || exit 1
mysqldump \
--all-databases \
--opt \
--single-transaction \
--source-data=2 \
--default-character-set=utf8 \
-h"node101" \
-u"root" \
-p"123456" | gzip > "${BACKUP_DIR}/$(date +%F).gz"

# 备份天数，超过这个值，最旧的备份会被删除
if [ "$(ls "${BACKUP_DIR}" | wc -l )" -gt "7" ]
then
  ls "${BACKUP_DIR}" | sort |sed -n 1p | xargs -I {} -n1 rm -rf "${BACKUP_DIR}"/{}
fi


